<?php
/**
 * @title 826. 安排工作以达到最大收益 - 排序+二分查找
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $difficulty
     * @param Integer[] $profit
     * @param Integer[] $worker
     * @return Integer
     */
    function maxProfitAssignment($difficulty, $profit, $worker) {
        /**
         * @since 2024-05-17 工作难度排序
         * @since 2024-05-17 在把工人分配到工作岗位后，我们所能获得的最大利润
         */
        array_multisort($difficulty, SORT_ASC, SORT_NUMERIC, $profit);
        $maxProfit = 0;

        /**
         * @since 2024-05-17 确保难度越高，收益越大
         */
        $lastProfit = 0;
        foreach ($profit as $k=>$p){
            if($p <= $lastProfit){
                unset($profit[$k], $difficulty[$k]);
            } else {
                $lastProfit = $p;
            }
        }
        $difficulty = array_values($difficulty);
        $profit = array_values($profit);

        /**
         * @since 2024-05-17 遍历工人能力，二分查找
         */
        foreach ($worker as $w){
            $s = 0;
            $e = count($difficulty)-1;

            while($s<=$e){
                $m = ($s+$e)>>1;
                $v = $difficulty[$m];

                if($v > $w){
                    $e = $m-1;
                } else {
                    $s = $m+1;
                }
            }
            if($s > 0){
                $maxProfit += $profit[$s-1];
                echo "work={$w}, difficulty=", $difficulty[$s-1], PHP_EOL;
            } else {}
        }

        /**
         * @return
         */
        return $maxProfit;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202405/2024.05.17.php
 */
$datas = [
    [[68,35,52,47,86], [67,17,1,81,3], [92,10,85,84,82]], // 324
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->maxProfitAssignment(...$data);
    echo $xhprof->DumpArgs([$result]);
    var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
